//全局变量

var common_option  = null;  //自定义
var is_readonly = false; 	//加载的状态
var is_rander_select = false;	
var is_modal_show = false;
var is_ajax_over = true ;  //ajax加载状态
var cur_data = null ; 
var id_name = pk_key;
var submit_table_url = null;
var isMark = false;  //开启标记位置的开关
var editorVal = "";
var is_renderMap = false; //map加载开关
var is_renderRichtext = false; //map加载开关

function randerModal(title,id){  //渲染浮层
    $("#table_detail .modal-title").text(title); //加载状态title
    var pk_id =id ; // 当前数据的id
    var data_item = null ; 
    if(pk_id==-1){ //判断当前数据的id
      	data_item={};
    }else{
      	for(var i=0; i<data_list.length; i++ ){
	        if(data_list[i][pk_key]==pk_id){ //找到相对应的数据
				data_item = data_list[i] ;  //data_items 当前的数据
				break ; 
	        }
      	}
    	var html = '';
		    html += '<div class="form-group">';
		    html += '<label class="col-sm-2 control-label">id:</label>';
		    html += '<div class="col-sm-6">';
		    html += '<p class="form-control-static">'+pk_id+'</p>';
		    html += '<input class="form-control-static" id="'+pk_id+'" type="hidden" name="goods_id" value="'+pk_id+'">';
		    html += '</div>';
		    html += '</div>';
  		$("#table_detail .modal-body #list_form_in").append(html); //渲染当前的数据的id
    }
    if(data_item!=null){ //判断当前数据
		cur_data = data_item ; 
		for(var c = 0 ; c < column_title.length ; c ++){
			var col = column_title[c] ; //title的数据
			var name = data_item[col['key']] ;  //数据所对应title的key
			if(col.editable == "1"||col.readonly=="1"){	//判断词条数据是否可被修改
				if(col.type == "number"){	//number类型
				  	editable_number(col,name);
				}else if(col.type == "text"){	//text类型
				  	editable_text(col,name);
				}else if(col.type == "time"){	//time类型
				  	editable_time(col,name);
				}else if(col.type == "enum"){	//enum类型
				  // var name = data_item[col['show_key']] ; 
				  // editable_enum(col,name);
				}else if(col.type == "custom"){	//custom类型
				  	editable_custom(col,name);
				}else if(col.type == "array"){	//array类型
				  	editable_array(col,name);
				}else if(col.type == "img"){	//img类型
			 	 	editable_img(col,data_item[col['show_key']]);
				}else if(col.type == "richtext"){	//richtext类型
				  	editable_richtext(col,name);
				}else if(col.type == "location"){	//location类型
				  	editable_location(col,name,data_item);
				}
			}
		}
		initEnumCol(data_item,column_title); //联动函数
    } 
}

//--------------------------------页面加载完成之后的事件
$(function(){
	$(".new_file_coupon").click(function(){ //新建按钮
		is_readonly = false ; 
		randerModal('新建',-1); //渲染浮层
		if($("#pageTitle").hasClass("nourl")){
			submit_table_url = "/"+data_name+"/create";
		}else{
			submit_table_url = "/ajax/call/"+data_name+"/create";
		}
	})
	$('.edit_btn').click(function(){  //编辑按钮
		var id = $(this).attr("data-pk");
			is_readonly = false ; 
			is_rander_select= false ; 
		randerModal('修改',$(this).attr("data-pk")); //渲染浮层
		if($("#pageTitle").hasClass("nourl")){
			submit_table_url = "/"+data_name+"/update?"+id_name+"="+id;
		}else{
			submit_table_url = "/ajax/call/"+data_name+"/update?"+id_name+"="+id;
		}
	});
	$('.detail_btn').click(function(){  //详情按钮
		is_readonly = true ; 
		is_rander_select= true ; 
		randerModal('查看',$(this).attr("data-pk")); //渲染浮层
		$(".submit_btn").hide();
	});
	$(".del_btn").click(function(){ //删除
		var id = $(this).attr("data-pk");
		swal({
			title: "确定要删除这条信息吗",
			text: "删除后将无法恢复，请谨慎操作！",
			type: "warning",
			showCancelButton: true,
			confirmButtonColor: "#DD6B55",
			confirmButtonText: "删除",
			cancelButtonText: "取消",
			closeOnConfirm: false
			}, function () {
			tabDel(id);
		});
	});
	$('#table_detail').on('shown.bs.modal', function () { //表单数据加载完之后
		is_modal_show = true ; 
		doRanderSelect();
		renderImg();	//渲染图片插件
		// renderLocation(); //渲染地图api插件   需要判断是否被执行
		if(is_renderMap){
			renderLocation();
		}
		if(is_renderRichtext){
			renderEditor();
		}
		if(common_option!=null){
			setTimeout(function(){
				if(common_option.complete!=undefined && common_option.complete!=null && typeof common_option.complete =='function'){
					try{
						common_option.complete();
					}catch(e){

					}
				}
			},1000)
		}
	});
	$('#table_detail').on('show.bs.modal', function () {	//表单数据加载完之前
		if(is_readonly){
			$(".rander_model_input").attr('readOnly',is_readonly);
			$(".rander_model_input").attr('disabled',is_readonly);
		}
		is_modal_show = false ;
		is_ajax_over = true ; 
	});
	$(".close").click(function(){ //表格上面关闭按钮
		$(".submit_btn").show();
		$("#table_detail .modal-body #list_form_in").html("");
		// $("#ueditor_textarea_editorValue").remove();
	});
	$(".table_close").click(function(){//表格下面关闭按钮
		$(this).attr("aria-hidden","true").attr("data-dismiss","modal")
		$("#table_detail .modal-body #list_form_in").html("");
		$(".submit_btn").show();
	});
	$(".submit_btn").click(function(){  //提交按钮
		$("#signupForm").validate({
			submitHandler:function(form){
				doSubmit(submit_table_url);
			}    
		});
	})
	$(".excel_submit_btn").click(function(){
		var loc_search = location.search;
		var url = null;
		var excel_name = $("#excel_name").val();
		if(loc_search==null || loc_search==""){
			url = "/excel/call"+location.pathname+"?excel_name="+excel_name;
		}else{
			url = "/excel/call"+location.pathname+loc_search+"&excel_name="+excel_name;
		}
		if(excel_name){
			location.href = url;
			// setTimeout(function(){
			// 	location.reload();
			// },1000)
		}else{
			$("#excel_name").focus();
		}
	})
});    
//---------------------------
function tabDel(id){  //删除
	var url = null;
	if($("#pageTitle").hasClass("nourl")){
		url = "/"+data_name+"/delete?"+id_name+"="+id;
	}else{
		url = "/ajax/call/"+data_name+"/delete?"+id_name+"="+id;
	}
	$.ajax({
		url:url,
		success:function(data){
			if(data.errno!=undefined && data.errno != 0 && typeof data != "string"){
		    	swal("很遗憾", data.errmsg+",请稍后重试");
			}else{
				if(typeof data == "string" ){
				    if(data.indexOf("nopermission_yes") >= 0){
				      	swal("系统提示", "您没有权限操作！", "error");
				      	return;
				    }
				}
				swal("删除成功！", "您已经删除了这条信息。", "success");
				location.reload();
			}
		}	
	})

}
function doSubmit(url){  //提交
$('#signupForm').ajaxSubmit({  
    type:"post",  //提交方式  
    url:url, //请求url  
    success:function(data){ //提交成功的回调函数  
      if(data.errno!=undefined && data.errno != 0 && typeof data != "string"){
        swal("很遗憾", data.errmsg+",请稍后重试");
      }else{
        if(typeof data == "string" ){
            if(data.indexOf("nopermission_yes") >= 0){
              swal("系统提示", "您没有权限操作！", "error");
              return;
            }
        }
        swal("恭喜您！", "提交成功啦。");
        location.reload();
      }
      
    }
});
}
//---------------
function switchMarkSite(){//开启与关闭标记位置
	isMark=!isMark;
	if(isMark){
		$("#id_btn_vender_site").html("已开启标记位置");
		$("#id_btn_vender_site").removeClass("btn-danger");
		$("#id_btn_vender_site").addClass("btn-success");
	}else{
		$("#id_btn_vender_site").html("已关闭标记位置");
		$("#id_btn_vender_site").addClass("btn-danger");
		$("#id_btn_vender_site").removeClass("btn-success");
	}
}

var  ue_inx = 0 ; 
//------------------------------------------渲染html元素---------------------------------------
function editable_richtext(col,val){  //渲染富文本类型的
	val = val==undefined?'':val; 
	var is_must = null;
	var required = null;
		editorVal = val;
	if(col.is_must && col.is_must == 1){
		is_must = '<span class="is_must">*</span>';
		required = 'required="true"';
	}else{
		is_must = '<span></span>';
		required = '';
	}

	var is_read = is_readonly ; 
	if(col.readonly && col.readonly == "1"){
		is_read = true;
	}
	var disStr = is_read?'disabled=""':'';
	var html = '';
		html += '<div class="form-group">';
		html += '<label class="col-sm-2 control-label"">'+col.name+':</label>';
		html += '<div class="col-sm-10 ">';
		html += '<textarea name="'+col.key+'" class="editor_h" '+disStr+' rows="20" id="'+col.key+'" style="display:none;">'+val+'</textarea>';
		html += '<script id="editor'+ue_inx+'" type="text/plain" style="width:100%;height:auto; min-height:300px;"></script>';
		// html += '<input type="hidden" name="'+col.key+'" id="'+col.key+'" class="editor_h" value="'+val+'" class="form-control" size="20" />';

		html += '</div>';
		html += '</div>';
	$("#table_detail .modal-body #list_form_in").append(html);
	is_renderRichtext = true;
}



function renderEditor(){
	


	/*********/
	var ue = UE.getEditor('editor'+ue_inx);
	$("#edui_fixedlayer").css("z-index","99999");
	ue.ready(function() {
4		//设置编辑器的内容
		ue.setContent($(".editor_h").val());
		//获取html内容，返回: <p>hello</p>
		var html = ue.getContent();
		//获取纯文本内容，返回: hello
		var txt = ue.getContentTxt();
		ue.on('contentchange', function () {
		    $(".editor_h").val(ue.getContent());
		});
		$("#edui2 div").click(function(){
			$("#edui_fixedlayer").css("z-index","99999");
		})
	});

	ue_inx++;
}
function editable_location(col,val,data_item){ //渲染地图类型的
	val = val==undefined?'':val; 
	var is_must = null;
	var required = null;
	if(col.is_must && col.is_must == 1){
		is_must = '<span class="is_must">*</span>';
		required = 'required="true"';
	}else{
		is_must = '<span></span>';
		required = '';
	}
	var html = '';
		html += '<div class="form-group">';
		html += '<label class="col-sm-2 control-label">'+col.name+':</label>';
		html += '<div class="col-sm-10 ">';
		html += '<div class="row" style="margin-bottom: 10px;">';
		html += '<div class="col-sm-4">';
		html += '<div class="input-group">';
		html += '<span id="id_mark_span" class="input-group-addon input-sm">轻点地图标记商家位置</span>';
		html += '<button class="btn form-control rander_model_input" type="button" onclick="switchMarkSite()" id="id_btn_vender_site">开启标记位置</button>';
		html += '</div>';
		html += '</div>';
		html += '<div id="r-result" class="col-sm-4">';
		html += '<div class="input-group">';
		html += '<span class="input-group-addon input-sm">位置搜索</span>';
		html += '<input type="text" id="suggestId" class="form-control rander_model_input" size="20" />';
		html += '</div>';
		html += '</div>';
		html += '</div>';
		html += '<div id="searchResultPanel" style="border: 1px solid #C0C0C0; width: 150px; height: auto; display: none;"></div>';
		html += '<div id="allmap" style="width: 100%; height:500px; margin-top: 20px;"></div>';
		html += '<input type="hidden" name="'+col.customMap.lng+'" id="'+col.customMap.lng+'" class="map_lng" value="'+data_item[col.customMap.lng]+'" class="form-control" size="20" />';
		html += '<input type="hidden" name="'+col.customMap.lat+'" id="'+col.customMap.lat+'" class="map_lat" value="'+data_item[col.customMap.lat]+'" class="form-control" size="20" />';
		html += '</div>';
		html += '</div>';
	$("#table_detail .modal-body #list_form_in").append(html);
	is_renderMap = true;
}
function renderLocation(){ //渲染地图掺进api
	// 百度地图API功能
	function G(id) {
		return document.getElementById(id);
	}
	var map = new BMap.Map("allmap");
	map.centerAndZoom("北京",12);                   // 初始化地图,设置城市和地图级别。
	var ac = new BMap.Autocomplete({    //建立一个自动完成的对象
			"input" : "suggestId",
			"location" : map
		});
	// map.enableScrollWheelZoom();   //启用滚轮放大缩小，默认禁用
	map.enableContinuousZoom();    //启用地图惯性拖拽，默认禁用
	var opts = {type: BMAP_NAVIGATION_CONTROL_LARGE}   // 
	map.addControl(new BMap.NavigationControl(opts));	//地图缩放控件
	map.addControl(new BMap.MapTypeControl()); //添加地图类型控件
	ac.addEventListener("onhighlight", function(e) {  //鼠标放在下拉列表上的事件
	  	var str = "";
	    var _value = e.fromitem.value;
	    var value = "";
	    if (e.fromitem.index > -1) {
	      	value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
	    }    
	    str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;
	    value = "";
	    if (e.toitem.index > -1) {
	      	_value = e.toitem.value;
	      	value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
	    }    
	    str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
	    G("searchResultPanel").innerHTML = str;
	});
	  	var myValue;
		ac.addEventListener("onconfirm", function(e) {    //鼠标点击下拉列表后的事件
	  	var _value = e.item.value;
	    myValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
	    G("searchResultPanel").innerHTML ="onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;
	    setPlace();
	});
	function setPlace(){
	    map.clearOverlays();    //清除地图上所有覆盖物
	    function myFun(){
	      	var pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
	      	map.centerAndZoom(pp, 18);
	      	map.addOverlay(new BMap.Marker(pp));    //添加标注
	    }
	    var local = new BMap.LocalSearch(map, { //智能搜索
	      	onSearchComplete: myFun
	    });
	    local.search(myValue);
	}
	//单击获取点击的经纬度
	map.addEventListener("click",function(e){
		if(isMark){
			// alert(e.point.lng + "," + e.point.lat);
			markVenderSite(e.point);
			$("#map_lng").val(e.point.lng);
			$("#map_lat").val(e.point.lat);
		}
	});
	var venderPoint = null; 
	function markVenderSite(pt){
	    removeAllMark();
	    //创建小狐狸
	    var myIcon = new BMap.Icon("https://o0iidk644.qnssl.com/op%2Flocation_admin_icon.png?imageView2/1/w/20/h/30/interlace/1", new BMap.Size(32,32));
	    var marker2 = new BMap.Marker(pt,{icon:myIcon});  // 创建标注
	    var label = new BMap.Label("我是商家");
	    marker2.setLabel(label);
	    venderPoint = pt;
	    map.addOverlay(marker2);              // 将标注添加到地图中
	    backPoint = pt; 
	}
	function removeAllMark(){
	    var allOverlay = map.getOverlays();
	    for (var i = 0; i < allOverlay.length ; i++){
	        map.removeOverlay(allOverlay[i]);
	    }
	}
	var venderPoints = {'lng':$(".map_lng").val(),'lat': $(".map_lat").val()};
	var firstLoad = false; 
	map.addEventListener("tilesloaded",function(){
	  	if(!firstLoad){
	    	firstLoad = true;
	    	if(venderPoint!=null){
	      		markVenderSite(venderPoints);
	    	}
	  	}
	  	markVenderSite(venderPoints);
	});
}
function editable_img(col,val){  //渲染图片类型的
	val = val==undefined?'':val; 
	var is_must = null;
	var required = null;
	if(col.is_must && col.is_must == 1){
		is_must = '<span class="is_must">*</span>';
		required = 'required="true"';
	}else{
		is_must = '<span></span>';
		required = '';
	}
	var is_read = is_readonly ; 
	if(col.readonly && col.readonly == "1"){
		is_read = true;
	}
	var disStr = is_read?'disabled=""':'';
	var html = '';
		html += '<div class="form-group">';
		html += '<label class="col-sm-2 control-label"">'+col.name+':</label>';
		html += '<div class="col-sm-6">';
		html += '<input id="'+col.key+'" class="rander_model_input pluginUnit_img" data-url="'+val+'" name="image_file" '+disStr+' value="'+val+'" type="file" accept="image/*">';   
		html += '</div>';      
		html += '<input id="'+col.key+'_ed" '+required+' type="hidden" name="'+col.show_key+'" value="'+val+'" accept="image/*">';   
		html += '</div>';
	$("#table_detail .modal-body #list_form_in").append(html);
}

function renderImg(){ //渲染图片插件
	var pluginUnit_img = $(".pluginUnit_img");
	$(".pluginUnit_img").each(function(){
		var venderImgLogoPath = $(this).attr("data-url");
	    var initalPreview = "";
	    if(venderImgLogoPath){
	      	initalPreview = "<img src='" + venderImgLogoPath + "' class='file-preview-image'>";
	    }
	    $(this).fileinput({
			showUpload: false,
			browseLabel: "选择图片",
			allowedFileExtensions: ["jpg", "jpeg", "gif", "png"],
			initialPreview: initalPreview
	    });
	    $(this).change(function(){ //上传图片
            var hid = $(this).attr("id")+"_ed";
            var fd = new FormData();
            var file_data = $(this).prop("files")[0];
            fd.append("image_file", file_data);
            $.ajax({
                url: '/upload/img',
                type: 'POST',
                cache: false,
                data: fd,
                processData: false,
                contentType: false,
                success: function (data) { 
                    if (data.errno == 0) {  
                       $("#"+hid).val(data.url);
                       swal("恭喜您！", "上传图片成功。");
                    }  
                    else{  
                       swal("出错啦！", "上传文件失败"); 
                    } 
                },
                 error: function () {
                   swal("出错啦！", "上传文件失败"); 
                }
            });
        })
	});
}
function editable_array(col,val){ //渲染数组类型
	val = val==undefined?'':val; 
	var ajaxUrl = col.customMap.url ; 
	var fdStart = ajaxUrl.indexOf("/");
	var url = null;
	if(fdStart == 0){
		url = ajaxUrl;
	}else{
		url = "/ajax/call/"+ajaxUrl;
	}
	$.ajax({
		url:url,
		type:'get',
		success:function(res){
			if(res.data_list!=undefined){
				editable_array2(col,val,res.data_list);
			}
		}    
	});
}
function editable_array2(col,val,dlist){ //渲染数组类型
	val = val==undefined?'':val; 
	var is_must = null;
	var required = null;
	if(col.is_must && col.is_must == 1){
		is_must = '<span class="is_must">*</span>';
		required = 'required="true"';
	}else{
		is_must = '<span></span>';
		required = '';
	}
	var is_read = is_readonly ; 
	if(col.readonly && col.readonly == "1"){
		is_read = true;
	}
	var disStr = is_read?'disabled=""':'';
	var html = '';
		html += '<div class="form-group">';
		html += '<label class="col-sm-2 control-label"">'+col.name+':</label>';
		html += '<div class="col-sm-6">';
		html += '<select id="'+col.key+'" name="'+col.key+'[]" '+disStr+' data-placeholder="请选择'+col.name+'" class="chosen-select col-sm-12" multiple tabindex="4">';
	for(var i =0 ; i < dlist.length ; i ++){
		var dd = dlist[i] ; 
		var sed = false ; 
		if(val!=undefined&&val!=null&&val.length>0){
			for(var z = 0 ; z < val.length ; z++){
				var v = val[z] ;
				if(v==dd[col.customMap.option_value]){
					sed = true ; 
					break ; 
				}
			}
		}
		if(sed){
			html += '<option value="'+dd[col.customMap.option_value]+'" selected hassubinfo="true">'+dd[col.customMap.option_text]+'</option>';
		}else{
			html += '<option value="'+dd[col.customMap.option_value]+'" hassubinfo="true">'+dd[col.customMap.option_text]+'</option>';
		}
	}
		html += '</select>';
		html += '</div>';
		html += '</div>';
	$("#table_detail .modal-body #list_form_in").append(html);
}
function editable_number(col,val){     //渲染浮层number类型的函数
	val = val==undefined?'':val; 
	var is_must = null;
	var required = null;
	if(col.is_must && col.is_must == 1){
		is_must = '<span class="is_must">*</span>';
		required = 'required="true"';
	}else{
		is_must = '<span></span>';
		required = '';
	}
	var is_read = is_readonly ; 
	if(col.readonly && col.readonly == "1"){
		is_read = true;
	}
	var disStr = is_read?'disabled=""':'';
	var html = '';
		html += '<div class="form-group">';
		html += '<label class="col-sm-2 control-label">'+col.name+':</label>';
		html += '<div class="col-sm-6">';
		html += '<input id="'+col.key+'" name="'+col.key+'" type="number" '+disStr+' class="form-control form-control rander_model_input" value="'+val+'" placeholder="请输入'+col.name+'" '+required+'>';
		html += '</div>'+is_must+'';
		html += '</div>';
	$("#table_detail .modal-body #list_form_in").append(html);
}
function editable_text(col,val){     //渲染浮层text类型的函数
	// alert('hehehe');
	val = val==undefined?'':val; 
	var is_must = null;
	var required = null;
	if(col.is_must && col.is_must == 1){
		is_must = '<span class="is_must">*</span>';
		required = 'required="true"';
	}else{
		is_must = '<span></span>';
		required = '';
	}
	var is_read = is_readonly ; 
	if(col.readonly && col.readonly == "1"){
		is_read = true;
	}
	var disStr = is_read?'disabled=""':'';
	
	var html = '';
		html += '<div class="form-group">';
		html += '<label class="col-sm-2 control-label">'+col.name+':</label>';
		html += '<div class="col-sm-6">';
		html += '<input id="'+col.key+'" name="'+col.key+'" type="text" class="form-control form-control rander_model_input" value="'+val+'" placeholder="请输入'+col.name+'" '+disStr+' '+required+' >';
		html += '</div>'+is_must+'';
		html += '</div>';
	$("#table_detail .modal-body #list_form_in").append(html);

}
function editable_time(col,val){       //渲染浮层time类型的函数
	val = val==undefined?'':val; 
	var is_must = null;
	var required = null;
	if(col.is_must && col.is_must == 1){
		is_must = '<span class="is_must">*</span>';
		required = 'required="true"';
	}else{
		is_must = '<span></span>';
		required = '';
	}
	var is_read = is_readonly ; 
	if(col.readonly && col.readonly == "1"){
		is_read = true;
	}
	var disStr = is_read?'disabled=""':'';
	var html = '';
		html += '<div class="form-group">';
		html += '<label class="col-sm-2 control-label">'+col.name+':</label>';
		html += '<div class="col-sm-6">';
		html += '<input id="'+col.key+'" class="form-control layer-date rander_model_input time_widget_laydate" data-hid-key="hid_'+col.key+'" '+disStr+' '+required+' placeholder="请选择日期" value="'+val+'"  onclick="laydate({istime: true, format:\'YYYY-MM-DD hh:mm:ss\',choose: function(dates){date_widget_choose(dates,\'hid_'+col.key+'\');}})">';
		html += '<input name="'+col.key+'" id="hid_'+col.key+'" class="data_hidden" type="hidden">';
		html += '</div>'+is_must+'';
		html += '</div>';
	$("#table_detail .modal-body #list_form_in").append(html);
	date_widget_choose($("#"+col.key).val(),'hid_'+col.key);
}
function editable_custom(col,val){       //渲染浮层custom类型的函数
	val = val==undefined?'':val; 
	var is_must = null;
	var required = null;
	if(col.is_must && col.is_must == 1){
		is_must = '<span class="is_must">*</span>';
		required = 'required="true"';
	}else{
		is_must = '<span></span>';
		required = '';
	}
	var is_read = is_readonly ; 
	if(col.readonly && col.readonly == "1"){
		is_read = true;
	}
	var disStr = is_read?'disabled=""':'';
	var html = '';
		html+= '<div class="form-group">';
		html+= '<label class="col-sm-2 control-label">'+col.name+':</label>  ';
		html+= '<div class="col-sm-6">';
		html+= '<select id="'+col.key+'" name="'+col.key+'" class="col-sm-12 form-control form-control m-b rander_model_input" width="" tabindex="" '+disStr+' '+required+'>';
		html+= getCustomOption(col,val);
		html+= '</select>';
		html+= '</div>'+is_must+'';
		html+= '</div>';
	$("#table_detail .modal-body #list_form_in").append(html);
}
function getCustomVal(col,val){  //获取当前custom类型选择的值
    if(col!=null&&col.customMap!=undefined &&col.customMap!=null){
        return col.customMap[val] ; 
    }else{
      	return "n/a";
    }
}
function getCustomOption(col,val){ //获取custom类型所有的值
	var htmStr = "" ; 
	if(col!=null&&col.customMap!=undefined &&col.customMap!=null){
		for(var k in col.customMap){
			if(k==val){
				htmStr+="<option selected='true' value='"+k+"'>"+col.customMap[k]+"</option>"
			}else{
				htmStr+="<option value='"+k+"'>"+col.customMap[k]+"</option>"
			}
		}
		return htmStr;    
	}else{
		return null;
	}
}
//-------------------------------------渲染enum类型的二级联动的系列操作---------

function doRanderSelect(){
	if(is_modal_show&&is_ajax_over){
		$('.chosen-select').chosen({});
		is_modal_show = false ;
		is_ajax_over = false; 
		// $(".dataTables-example").dataTable();
		$(".chosen-select").change(function(){
			var my_key = $(this).attr("data-key") ; 
			if($('.parent_'+my_key).length>0){
				var col_ary = [] ; 
				for(var i = 0  ; i < column_title.length ; i ++ ){  //有 parent 的 
					var col = column_title[i] ; 
					if(col.type=='enum'){
						if(col.customMap.enum_parent!=undefined&&col.customMap.enum_parent!=null){
							col_ary.push(col);
						}
					}
				}
				$('.parent_'+my_key).remove();
				is_modal_show = true ; 
				reanderEnumCol(cur_data,col_ary);
			}
		});
	}
}
function initEnumCol(dt,ct){
	var col_ary = [] ; 
	for(var i = 0  ; i < ct.length ; i ++ ){  //有 parent 的 
		var col = ct[i] ; 
		if(col.type=='enum'){
			if(col.customMap.enum_parent!=undefined&&col.customMap.enum_parent!=null){
				col_ary.push(col);
			}
		}
	}
	for(var i = 0  ; i < ct.length ; i ++ ){  //没有parent 的 
		var col = ct[i] ; 
		if(col.type=='enum'){
			if(col.customMap.enum_parent==undefined||col.customMap.enum_parent==null){
				col_ary.push(col);
			}
		}
	}
	reanderEnumCol(dt,col_ary);
}
function reanderEnumCol(data,col_ary){
	if(col_ary.length>0){
		var cl = col_ary.pop();
		if(cl.enum_parent==undefined||cl.enum_parent==null){
			randenBaseEnumSelect(cl,data[cl.key],function(){
				reanderEnumCol(data,col_ary);
			});
		}else{
			randenBaseEnumSelect(cl,data[cl.key],function(){
				reanderEnumCol(data,col_ary);
			});
		}
	}else{
		is_ajax_over = true ; 
		doRanderSelect();
	}
}
function editable_enum(col,val,dlist){
    var my_key = col.key ; 
    var par_key = (col.customMap.enum_parent!=undefined&&col.customMap.enum_parent!=null)?('parent_'+col.customMap.enum_parent):'';
    var params_key = (col.customMap.params!=undefined&&col.customMap.params!=null&&col.customMap.params.length>0)?col.customMap.params[0]:'';
    var is_must = null;
    var required = null;
        if(col.is_must && col.is_must == 1){
          is_must = '<span class="is_must">*</span>';
          required = 'required="true"';
        }else{
          is_must = '<span></span>';
          required = '';
        }
      	var html = '';
      	var is_read = is_readonly ; 
		if(col.readonly && col.readonly == "1"){
			is_read = true;
		}
		var disStr = is_read?'disabled=""':'';
          	html += '<div class="form-group '+par_key+' ">';
          	html += '<label class="col-sm-2 control-label">'+col.name+':</label>';
          	html += '<div class="col-sm-6">';
          	html += '<select id="'+col.key+'" class="chosen-select rander_model_input col-sm-12 '+my_key+'  "  '+disStr+' width="" data-key="'+my_key+'" data-params="'+params_key+'" data-parent="'+par_key+'" name="'+col.key+'" data-url="'+col.customMap.url+'" tabindex="2" '+required+'>';
          	if(col.customMap.default_value != undefined){
            	html += '<option value="'+col.customMap.default_value+'" >'+col.customMap.default_name+'</option>';
          	}
          	for(var i =0 ; i < dlist.length ; i ++){
              	var dd = dlist[i] ; 
              	if(val == dd[col.customMap.option_value]){
                	html += '<option value="'+dd[col.customMap.option_value]+'" selected hassubinfo="true">'+dd[col.customMap.option_text]+'</option>';
              	}else{
                	html += '<option value="'+dd[col.customMap.option_value]+'" hassubinfo="true">'+dd[col.customMap.option_text]+'</option>';
              	}
          	}
			html += '</select>';
			html += '</div>'+is_must+'';
			html += '</div>';
      $("#table_detail .modal-body #list_form_in").append(html);
}

function randenBaseEnumSelect(col,val,callback){
	if(col!=null&&col.customMap!=undefined && col.customMap!=null){
		var par = {} ; 
		if(col.customMap.enum_parent!=undefined&&col.customMap.enum_parent!=null){
			if(col.customMap.params!=undefined&&col.customMap.params.length>0){
				par[col.customMap.params[0]] = $('.'+col.customMap.enum_parent).val() ; 
			}
		}
		is_ajax_over = false ; 
		var ajaxUrl = col.customMap.url ;
		var fdStart = ajaxUrl.indexOf("/");
		var url = null;
		if(fdStart == 0){
			url = ajaxUrl;
		}else{
			url = "/ajax/call/"+ajaxUrl;
		}
		$.ajax({
			url:url,
			type:'get',
			data:par,
			success:function(res){
				if(res.data_list!=undefined){
					editable_enum(col,val,res.data_list);
				}
				if(callback!=null&&typeof callback == 'function'){
					callback();
				}
			}    
		});
	}
}
function date_widget_choose(dates,hid){
  	dates = dates.replace(/[^0-9]/ig, "");
  	$('#'+hid).val(dates); 
}

$(".imgTab_btn").click(function(){
	var imgSrc = $(this).children('img').attr('src');
	var wrap_left = "";
	var wrap_top = "";
	var html = "";
		html += '<div class="list_enlargeImg" onclick="closeEnlargeImg(event);">';
		html += '<div class="enlarge_wrap">';
		html += '<img  src="'+imgSrc+'">';
		html += '<span class="close_btn fa fa-close" onclick="closeEnlargeImg(event);"></span>';
		html += '</div>';
		html += '</div>';
	$('html').append(html);
	wrap_left = "-"+($(".enlarge_wrap").width()+40)/2;
	wrap_top = "-"+($(".enlarge_wrap").height()+40)/2;
	$(".enlarge_wrap").css({"margin-top":wrap_top+'px',"margin-left":wrap_left+'px'});
	

});

function closeEnlargeImg(event){
	if(event.target.localName != 'img' && event.target.className != 'enlarge_wrap'){
		$(".list_enlargeImg").fadeOut(function(){
			$(".list_enlargeImg").remove();
		});
	}
}

function closeEnlargeImg2(event){
	$(".list_enlargeImg").fadeOut(function(){
		$(".list_enlargeImg").remove();
	})
	
}






































































